#This is an example of making bar plots # the first plot comes from data given on the # the background page for the associated video # We want to get the values that we will plot # into our script. Use the c() function to # create our variable holding the values fn_length <- c( 7, 47, 93, 117, 95, 57, 24, 8, 4) # then, in its most basic form we can get a plot barplot( fn_length ) # of course, it would be nice to get labels on the # bars. To do this we will assign names to the # values in our variable fn_length names(fn_length) <- c("Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "over ten") # and then create a new plot barplot( fn_length ) # *********************************************** # *** This is as fancy as you need to get *** # *** for any bar plot reuired in this *** # *** class. The rest is just showing off. *** # *********************************************** # Now I want to extend the vertical scale, To do this # we mmake our command a bit more complex barplot( fn_length, ylim=c(0,140)) # And we should put a title on this graph barplot( fn_length, ylim=c(0,140), main="Length of First Names for Math 160 Students") # And, to make this a bit easier to read # we will add a bottom line and then add some dotted # horizontal lines. abline(h=0, lty="solid") abline(h=seq(0, 140, 20), lty="dotted") # With a little planning we could get those lines in # the background. First, generate a new graph. barplot( fn_length, ylim=c(0,140), main="Length of First Names for Math 160 Students") abline(h=0, lty="solid") abline(h=seq(0, 140, 20), lty="dotted") # Then give this strange command which does seem backwards # but when new is TRUE a new command will write over the # existing plot par( new=TRUE) # so now just draw the plot again. barplot( fn_length, ylim=c(0,140), main="Length of First Names for Math 160 Students") par( new=FALSE ) # # We could start over, but this time get a horizontal graph barplot( fn_length, horiz=TRUE, main="Length of First Names for Math 160 Students") # This time we should increase the bottom scale barplot( fn_length, horiz=TRUE, xlim=c(0,140), main="Length of First Names for Math 160 Students") # And the names along the left just do not fit because # of their direction with respect to the axis. We can # change that so that the names are perpendicular to # the axis barplot( fn_length, horiz=TRUE, xlim=c(0,140), las=1, main="Length of First Names for Math 160 Students") # And we can add some vertical lines abline( v=0, lty="solid") abline(v=seq(0,140,20), lty="dotted") par( new=TRUE) barplot( fn_length, horiz=TRUE, xlim=c(0,140), las=1, main="Length of First Names for Math 160 Students") # And, while we are making this a bit fancier let us # add some ccolor to it par( new=TRUE ) barplot( fn_length, horiz=TRUE, xlim=c(0,140), las=1, main="Length of First Names for Math 160 Students", col=rainbow(9)) par( new=FALSE ) ######################################################### # Now look at the second problem ######################################################### # Load gnrnd4 so that we can generate the values source( "../gnrnd4.R") # Then run gnrnd4 to actually create the values gnrnd4(745099103, 600031) # The values are in L1, inspect them to be sure # that we have the right values L1 # # We might think that we can just use the barplot() # functon on L1 but that will not work barplot(L1) # we have a bar for each value in the data and the # length (in this case height) of each bar is the # magnitude of the associated value. # but what we want is a bar plot that shows the frequency # of each different value in the data. We could try to # count the number of 35's, the number of 36's, and # so on, but we will almost certainly make at least one # mistake. So, let the computer do it for us. The # function table() will get those frequencies... table(L1) # So I see that there are 8 "32's", 21 "33's, and # so on. Now, I have the same kind of information # that we had in the first problem. We could do the # same steps and create a variable that holds the # frequencies and then plot that variable. But there # is a shorter way to do this, just give barplot() # the result of the table() function. barplot( table(L1) ) # And, we can fancy that up to our heart's content barplot( table(L1), ylim=c(0,25), main="Frequencies of values in the table", col=c("red","green","blue","orange","purple", "tan")) abline(h=0, lty="solid") abline(h=seq(0,25,5), lty="dotted", col="darkred") par(new=TRUE) barplot( table(L1), ylim=c(0,25), main="Frequencies of values in the table", col=c("red","green","blue","orange","purple", "tan"), xlab="Table values.") par( new=FALSE )